import router from './router'
import store from './store'
// import NProgress from 'nprogress'
// import 'nprogress/nprogress.css'
// 白名单页面
const whileList = ['/404', '/login']
// 启用前置守卫
router.beforeEach(async(to, from, next) => {
  // NProgress.start()
  const { token, userId } = store.getters
  console.log('router.beforeEach  token', token)

  // console.log('router.beforeEach  store.getters', store.getters)
  // console.log(token)
  if (token) {
    // 判断是否去登录页
    if (to.path === '/login') next('/')
    else if (whileList.includes(to.path)) next()
    else {
      if (!userId) await store.dispatch('user/getUserInfo')
      next()
    }
  } else {
    if (whileList.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
})
